package com.wixct.controller;

import com.wixct.common.HttpClient5Utils;
import com.wixct.repository.domain.TBlog;
import com.wixct.repository.service.TBlogService;
import jakarta.annotation.Resource;
import jakarta.servlet.http.HttpServletResponse;
import lombok.extern.slf4j.Slf4j;
import org.apache.hc.client5.http.ContextBuilder;
import org.apache.hc.client5.http.classic.methods.HttpPost;
import org.apache.hc.client5.http.impl.classic.CloseableHttpClient;
import org.apache.hc.client5.http.protocol.HttpClientContext;
import org.apache.hc.core5.http.ClassicHttpRequest;
import org.apache.hc.core5.http.io.entity.EntityUtils;
import org.apache.hc.core5.http.message.StatusLine;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.ResponseBody;

import java.io.IOException;

@Controller
@Slf4j
public class TblogController {

    @Resource
    private TBlogService tblogService;

    @GetMapping("/blog/{id}")
    @ResponseBody
    public TBlog geBlog(@PathVariable String id, HttpServletResponse response) {
        TBlog tBlog=tblogService.getById(id);
        log.info("id={}",id);
        response.setContentType("text/html;charset=utf-8");
        response.setStatus(HttpServletResponse.SC_OK);
        response.setCharacterEncoding("utf-8");
        if(tBlog!=null){
            log.info(tBlog.toString());
        }
        return tBlog;
    }

    @GetMapping("/api/hello")
    @ResponseBody
    public String hello(HttpServletResponse resp) throws IOException {
        CloseableHttpClient client = HttpClient5Utils.getHttpClient();
        HttpPost post = new HttpPost("https://www.baidu.com");
        final HttpClientContext context = ContextBuilder.create()
                .build();
        client.execute(post, context, response -> {
            System.out.println("----------------------------------------");
            System.out.println(post + "->" + new StatusLine(response));
            EntityUtils.consume(response.getEntity());
            return null;
        });
        return "success";
    }
}
